<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="方向名称">
          <el-input v-model="formInline.name" placeholder="方向名称"></el-input>
        </el-form-item>





        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="toAdd">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="id"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="方向"
          width="180">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template v-slot:="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
            <el-button type="text" @click="deletecollege(scope.row.id)" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.page"
        :page-sizes="[1,2,3,4]"
        :page-size="formInline.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <el-dialog title="修改方向" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="方向" :label-width="formLabelWidth">
            <el-input v-model="form.id" autocomplete="off"></el-input>
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updatecollege">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      formInline: {
        name: '',
        page: 1,
        size: 5


      },
      total: 10,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form:{
id:0,
        name:""
      }
    }
  },
  created() {
    this.showList()
  },
  methods: {
    deletecollege(id){
      this.$axios.get('college/delete?id='+id).then((res)=>{
        this.$message({
          message:'成功',
          type:'success'
        })
        this.showList()
      })
    },
    updatecollege(){
      this.$axios.post('college/update',this.form).then((res)=>{
        console.log(res)
        this.$message({
          message:'成功',
          type:'success'
        });
        this.dialogFormVisible =false
      })
    },
    handleClick(row) {
      this.dialogFormVisible =true,
        this.form.id = row.id
      this.form.name =row.name
    },
    toAdd(){
      this.$router.push("CollegeAdd")
    },
    showList() {
      this.$axios.post("college/searchPage", this.formInline).then((res) => {
        console.log(res.data.data)
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      })
    },
    onSubmit() {
      this.formInline.page=1
      this.showList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.formInline.size = val
      this.showList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formInline.page = val
      this.showList()
    }
  }
}
</script>

<style scoped>

</style>
